//
//  RealmTool.m
//  Daybook
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "RealmTool.h"
#import "DayBookModel.h"

@implementation RealmTool

+ (RLMResults *)getCurrentAccountFromUserDefaults{
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    NSString *userValue = [users stringForKey:@"accountBookIndex"];
    RLMRealm *realm = [RLMRealm defaultRealm];
    RLMResults *results = [DayBook objectsInRealm:realm where:@"accountBookIndex = %@", userValue];
//    NSLog(@"CurrentAccount: %@", results);
    return results;
}

+ (void)deleteDayBookInRealmFromIndex:(NSString *)index {
    RLMRealm *realm = [RLMRealm defaultRealm];
    RLMResults *result = [DayBook objectsInRealm:realm where:@"accountBookIndex = %@", index];
    [realm beginWriteTransaction];
    [realm deleteObjects:result];
    [realm commitWriteTransaction];
//    NSLog(@"alllobject: %@", [DayBook allObjects]);
}

+ (void)createDayBookInRealmFromIndex:(NSString *)index name:(NSString *)name{
    RLMRealm *realm = [RLMRealm defaultRealm];
    DayBook *dayBook = [[DayBook alloc] init];
    
    RLMResults *results = [DayBook objectsWhere:@"accountBookIndex = %@", index];
    dayBook.accountBookIndex = index;
    dayBook.accountBookName = name;
    [realm beginWriteTransaction];
    if (results.count == 0) {
        [realm addObject:dayBook];
    }
    [realm commitWriteTransaction];
//    NSLog(@"alllobject: %@", [DayBook allObjects]);
}

+ (NSString *)incomeStringFromCurrentAccountFromUserDefaults {
    RLMResults *currentResult = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [currentResult firstObject];
    
    RLMResults *accountBooks = (RLMResults *)currentDayBook.bookContent;
    double income = 0;
    for (Account *book in accountBooks) {
        income += book.income.doubleValue;
    }
    return [NSString stringWithFormat:@"%.2f", income];
}

+ (NSString *)payStringFromCurrentAccountFromUserDefaults {
    RLMResults *currentResult = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [currentResult firstObject];
    
    RLMResults *accountBooks = (RLMResults *)currentDayBook.bookContent;
    double pay = 0;
    for (Account *book in accountBooks) {
        pay += book.money.doubleValue;
    }
    return [NSString stringWithFormat:@"%.2f", pay];
}

/********************************************************************/

+ (void)createNewRealmDateBaseWithAccountName:(NSString *)name {
    //dbName:就是账号，每个账号对应一个dbName，这样，就能够每个账号对应生成一个Realm数据库；
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString *dbName  = [name stringByAppendingString:@".realm"];
    NSString *dbPath = [sandBox stringByAppendingPathComponent:dbName];
    NSFileManager *manager  =  [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:dbPath]) {
        RLMRealm *realm = [RLMRealm realmWithURL:[NSURL URLWithString:dbPath]];
        //这行代码就能够实现：如果该目录下没有以dbName命名的数据库存在，那么则生成该数据库，如果已经存在，则realm就是该数据库
    }
}

+ (RLMRealm *)getRealmFromAccountName:(NSString *)name {
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.realm", name]];
    RLMRealm *realm = [RLMRealm realmWithURL:[NSURL URLWithString:dbPath]];
    return realm;
}

/********************************************************************/

//传入需要切换成的数据库的名字username，则可以通过下列函数实现自定义数据库的切换
+ (void)setDefaultRealmForUser:(NSString *)username {
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    
    // 使用默认的目录，但是使用用户名来替换默认的文件名
//    config.path = [[[config.path stringByDeletingLastPathComponent]
//                    stringByAppendingPathComponent:username]
//                   stringByAppendingPathExtension:@".realm"];
    
    // 将这个配置应用到默认的 Realm 数据库当中
    [RLMRealmConfiguration setDefaultConfiguration:config];
}

@end
